package com.taptech.doufu.ui.view.choice.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.NovelModel;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.ui.activity.story.StoryDetailsActivity;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;
import com.taptech.doufu.util.StringUtil;
import com.taptech.doufu.util.TagsUtil;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class ChoiceTopicStoryAdapter extends RecyclerView.Adapter<ChoiceTopicStoryAdapter.ViewHolder>{

    private List<NovelModel> list;
    private Context context;
    private EventBean eventData;


    public ChoiceTopicStoryAdapter(Context context, List<NovelModel> list, EventBean eventData){
        this.eventData = eventData;
        this.list = list;
        this.context = context;
    }


    public void addDatas(List<NovelModel> list) {
        if (this.list != null && list != null) {
            this.list.addAll(list);
            notifyDataSetChanged();
        }
    }

    public NovelModel getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.topic_choice_story_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.title =  view.findViewById(R.id.title);
        holder.tvTag1 =  view.findViewById(R.id.tvTag1);
        holder.tvTag2 =  view.findViewById(R.id.tvTag2);
        holder.tvDesc =  view.findViewById(R.id.tvDesc);
        holder.tvAuthorDesc =  view.findViewById(R.id.tvAuthorDesc);
        holder.lay = view.findViewById(R.id.lay);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final NovelModel model = getItem(position);
        if (model != null) {
            holder.title.setText(model.getTitle());
            if (!TextUtils.isEmpty(model.getBrief())) {
                holder.tvDesc.setText(StringUtil.filterAllSpace(model.getBrief()));
            }else if(!TextUtils.isEmpty(model.getDescription())){
                holder.tvDesc.setText(StringUtil.filterAllSpace(model.getDescription()));
            }
            if (model.getUser() != null) {
                if (!TextUtils.isEmpty(model.getUser().getNickname())) {
                    holder.tvAuthorDesc.setText(model.getUser().getNickname());
                }
            }
            if ((model.getTags() != null) && (model.getTags().length > 0)) {
                if (model.getTags()[0] != null) {
                    holder.tvTag1.setText((model.getTags()[0]).getName());
                    holder.tvTag1.setVisibility(View.VISIBLE);
                    holder.tvTag1.setOnClickListener(v -> {
                        TagsUtil.openWeexTagActivity(context,model.getTags()[0].getName(), Constant.OBJECT_TYPE_NOVEL,1);
                    });
                }else{
                    holder.tvTag1.setVisibility(View.GONE);
                }
                if ((model.getTags().length > 1) && (model.getTags()[1] != null)) {
                    holder.tvTag2.setText((model.getTags()[1]).getName());
                    holder.tvTag2.setVisibility(View.VISIBLE);
                    holder.tvTag2.setOnClickListener(v -> {
                        TagsUtil.openWeexTagActivity(context,model.getTags()[1].getName(), Constant.OBJECT_TYPE_NOVEL,1);
                    });
                }else{
                    holder.tvTag2.setVisibility(View.GONE);
                }
            }
            changeDayModelBg(holder);
            holder.lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_CONTENT,eventData);
                    StoryDetailsActivity.Companion.startActivity(context,model.getId());

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        TextView tvTag1;
        TextView tvTag2;
        TextView tvDesc;
        TextView tvAuthorDesc;
        View lay;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.text_color_1));
        viewHolder.tvDesc.setTextColor(context.getResources().getColor(R.color.text_color_727885));
        viewHolder.tvAuthorDesc.setTextColor(context.getResources().getColor(R.color.text_color_8993A3));
        viewHolder.tvTag1.setTextColor(context.getResources().getColor(R.color.text_color_8993A3));
        viewHolder.tvTag1.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_tag_gray));
        viewHolder.tvTag2.setTextColor(context.getResources().getColor(R.color.text_color_8993A3));
        viewHolder.tvTag2.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_tag_gray));
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.tvDesc.setTextColor(context.getResources().getColor(R.color.text_dark));
        viewHolder.tvAuthorDesc.setTextColor(context.getResources().getColor(R.color.text_dark));
        viewHolder.tvTag1.setTextColor(context.getResources().getColor(R.color.text_dark));
        viewHolder.tvTag1.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_tag_gray_dark));
        viewHolder.tvTag2.setTextColor(context.getResources().getColor(R.color.text_dark));
        viewHolder.tvTag2.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_tag_gray_dark));
    }
}
